﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Clinica_Frba
{
    public partial class SeleccionarRol : Form
    {
        public Conexion sql = Conexion.getInstance();
        public Utility util = Utility.getInstance();
        
        private RadioButton radioButton;

        public SeleccionarRol()
        {
            InitializeComponent();
        }

        private void SeleccionarRol_Load(object sender, EventArgs e)
        {

        }

        public int initUserRoles(string user)
        {
            SqlDataReader reader;
            int count = 0;
            int pointY = 85;
            int tabIndex = 1;
            int idRol = 0;

            reader = sql.execQuery("SELECT rol.id_rol,Nombre " +
                                   " FROM b_sharp.Rol_Usuario rol" +
                                   " JOIN b_sharp.Rol de ON rol.id_rol = de.id_Rol" +
                                   " WHERE id_usuario = '" + user + "'" +
                                   " ;");
            while (reader.Read())
            {
                count++;
                tabIndex++;
                idRol = reader.GetInt32(0);
                this.radioButton = new RadioButton();
                
                this.radioButton.AutoSize = true;
                this.radioButton.Location = new System.Drawing.Point(29, pointY);
                this.radioButton.Name = Convert.ToString(reader.GetInt32(0));
                this.radioButton.Size = new System.Drawing.Size(110, 17);
                this.radioButton.TabIndex = tabIndex;
                this.radioButton.TabStop = true;
                this.radioButton.Text = reader.GetString(1);
                this.radioButton.UseVisualStyleBackColor = true;

                this.Controls.Add(this.radioButton);

                pointY += 23;
            }

            reader.Close();

            //Si solo tiene un rol asignado guardarlo en la clase global
            if (count == 1)
                util.setIdRol(idRol);

            return count;
        }

        private void buttonRol_Click(object sender, EventArgs e)
        {
            RadioButton radio;

            //Loopear los radiobuttons
            foreach (Control control in this.Controls)
            {
                if (control.GetType().ToString() == "System.Windows.Forms.RadioButton")
                {
                    radio = control as RadioButton;

                    if (radio.Checked)
                    {   //Guardar Id del rol seleccionado
                        util.setIdRol(Convert.ToInt32(radio.Name));
                        break;
                    }

                }
            }
            // Cerrar ventana
            this.Close();
        }
    }
}
